package com.algo.pattern.iterator;

import java.util.ArrayList;
import java.util.List;

public class Classroom {

    private List<Student> stuList;

    public Classroom() {
        stuList = new ArrayList<>();
    }

    public void addStudent(Student... students) {
        for (Student stu : students) {
            stuList.add(stu);
        }
    }

    public Iterator<Student> iterator() {
        return new StudentIterator(stuList);
    }

    /**
     * 迭代器实现
     */
    class StudentIterator implements Iterator<Student> {

        private List<Student> stuList;

        private int curIndex = -1;

        public StudentIterator(List<Student> stuList) {
            this.stuList = stuList;
        }

        @Override
        public boolean hasNext() {
            return stuList != null && ++curIndex < stuList.size();
        }

        @Override
        public Student next() {
            return stuList.get(curIndex);
        }
    }

}
